//
//  AXHdzqMsZzfqDetailVC.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/11/23.
//  Copyright © 2018 tzhg. All rights reserved.
//

#import "AXHdzqMsZzfqDetailVC.h"
#import "YXIgnoreHeaderTouchAndRecognizeSimultaneousTableView.h"
#import "YXTabView.h"
#import "YX.h"
#import "ZXCategories.h"
#import "UIView+SDAutoLayout.h"
#import "WHRProductDetailsCell.h"
#import "WHRProductActivityCell.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "UIViewController+HBD.h"
#import "LZProductDetails.h"
#import "AXHdzqMsZzfqDetailModel.h"
#import "PlayerView.h"
#import "UIView+SetRect.h"
#import "MsZzfqDetailSubmitOrderVC.h"
#import "WHRDiscountCouponVC.h"
#import "UIViewController+YCPopover.h"


@interface AXHdzqMsZzfqDetailVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong) NSMutableArray<AXHdzqMsZzfqDetailModel *> *modelsArray;
@property (nonatomic , strong) AXHdzqMsZzfqDetailModel *detailModel1;
@property (nonatomic, strong) YXIgnoreHeaderTouchAndRecognizeSimultaneousTableView *tableView;

@property (nonatomic, assign) BOOL isTopIsCanNotMoveTabView;

@property (nonatomic, assign) BOOL isTopIsCanNotMoveTabViewPre;

@property (nonatomic, assign) BOOL canScroll;





@end

@implementation AXHdzqMsZzfqDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavigationItems];
    [self initUI];
    self.view.backgroundColor = [UIColor whiteColor];
    self.hbd_barTintColor = [UIColor whiteColor];
    self.hbd_barShadowHidden = YES;
    


    self.tableView.tableHeaderView.userInteractionEnabled = YES;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestDetailInfo];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self requestDetailInfo];
        
    }];
    
    LKEmptyViewConfig *config = [LKEmptyViewConfig setupEmptyViewConfigForTableView:self.tableView];
    config.message = @"暂无数据";
    
    [self.tableView.mj_header beginRefreshing];

}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    for (UIView *view in self.detailsV.subviews) {
        
        if ([view isKindOfClass:[PlayerView class]]) {
            PlayerView *playerView = (PlayerView *)view;
            [playerView pausePlay];
        }
    }
}


- (void)setupNavigationItems {
    
    /** 导航条左边的按钮 */
    //返回按钮
    UIButton *kfBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [kfBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    kfBtn.frame = CGRectMake(0, 0, 30, 30);
    [kfBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:kfBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    /** title */
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    titleLabel.text = @"商品详情";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = FONT_EIGHTEEN;
    titleLabel.textColor = [UIColor colorWithHex:0x29b566];
    self.navigationItem.titleView = titleLabel;
    
}

- (void)backAction:(UIBarButtonItem *)item {
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)initUI{
    /** 表头 */
   
    
    _tableView = [[YXIgnoreHeaderTouchAndRecognizeSimultaneousTableView alloc] initWithFrame:CGRectMake(0, -20, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-kBottomBarHeight) style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.tableFooterView = [UIView new];
    
    //表头

    [_tableView registerClass:[WHRProductDetailsCell class] forCellReuseIdentifier:[WHRProductDetailsCell reuseID]];
    [_tableView registerClass:[WHRProductActivityCell class] forCellReuseIdentifier:[WHRProductActivityCell reuseID]];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    [self initBottomView];
    

    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:kLeaveTopNotificationName object:nil];
}

-(void)acceptMsg : (NSNotification *)notification{
    
    NSDictionary *userInfo = notification.userInfo;
    NSString *canScroll = userInfo[@"canScroll"];
    if ([canScroll isEqualToString:@"1"]) {
        _canScroll = YES;
    }
}

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}



-(void)initBottomView{
    
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame)-kBottomBarHeight, CGRectGetWidth(self.view.frame), kBottomBarHeight)];
    [self.view addSubview:bottomView];
   
    UIView *leftView = [[UIView alloc]init];
    leftView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [bottomView addSubview:leftView];
    
    leftView.sd_layout
    .leftSpaceToView(bottomView, 0)
    .topSpaceToView(bottomView, 0)
    .widthIs(150)
    .heightIs(kBottomBarHeight);
    
    UIView *view1 = [[UIView alloc]init];
    [leftView addSubview:view1];
    
    view1.sd_layout
    .leftSpaceToView(leftView, 0)
    .topSpaceToView(leftView, 0)
    .widthIs(50)
    .heightIs(60);
    

    UIButton *btn1 = [[UIButton alloc]init];
    [btn1 setImage:[UIImage imageNamed:@"sc"] forState:UIControlStateNormal];
    [btn1 setImage:[UIImage imageNamed:@"unselectSc"] forState:UIControlStateSelected];
    [view1 addSubview:btn1];
    
    btn1.sd_layout
    .leftSpaceToView(view1, 10)
    .topSpaceToView(view1, 5)
    .widthIs(30)
    .heightIs(30);
    
    
    UILabel *label1 = [[UILabel alloc]init];
    label1.textAlignment = NSTextAlignmentCenter;
    label1.font = [UIFont systemFontOfSize:12];
    label1.text = @"收藏";
    label1.textColor = COLOR_GREEN;
    [view1 addSubview:label1];
    
    label1.sd_layout
    .leftSpaceToView(view1, 0)
    .topSpaceToView(btn1, 5)
    .widthIs(50)
    .heightIs(20);
    
    UIView *view2 = [[UIView alloc]init];
    [leftView addSubview:view2];
    
    view2.sd_layout
    .leftSpaceToView(view1, 0)
    .topSpaceToView(leftView, 0)
    .widthIs(50)
    .heightIs(60);
    
    
    UIButton *btn2 = [[UIButton alloc]init];
    [btn2 setImage:[UIImage imageNamed:@"sp"] forState:UIControlStateNormal];
    [view2 addSubview:btn2];
    
    btn2.sd_layout
    .leftSpaceToView(view2, 10)
    .topSpaceToView(view2, 5)
    .widthIs(30)
    .heightIs(30);
    
    
    UILabel *label2 = [[UILabel alloc]init];
    label2.textAlignment = NSTextAlignmentCenter;
    label2.font = [UIFont systemFontOfSize:12];
    label2.text = @"店铺";
    
    label2.textColor = COLOR_GREEN;
    [view2 addSubview:label2];
    
    label2.sd_layout
    .leftSpaceToView(view2, 0)
    .topSpaceToView(btn2, 5)
    .widthIs(50)
    .heightIs(20);
    
    
    UIView *view3 = [[UIView alloc]init];
    [leftView addSubview:view3];
    
    view3.sd_layout
    .leftSpaceToView(view2, 0)
    .topSpaceToView(leftView, 0)
    .widthIs(50)
    .heightIs(60);
    
    
    UIButton *btn3 = [[UIButton alloc]init];
    [btn3 setImage:[UIImage imageNamed:@"navkf"] forState:UIControlStateNormal];
    [view3 addSubview:btn3];
    
    btn3.sd_layout
    .leftSpaceToView(view3, 10)
    .topSpaceToView(view3, 5)
    .widthIs(30)
    .heightIs(30);
    
    
    UILabel *label3 = [[UILabel alloc]init];
    label3.textAlignment = NSTextAlignmentCenter;
    label3.font = [UIFont systemFontOfSize:12];
    label3.text = @"客服";
    label3.textColor = COLOR_GREEN;
    [view3 addSubview:label3];
    
    label3.sd_layout
    .leftSpaceToView(view3, 0)
    .topSpaceToView(btn3, 5)
    .widthIs(50)
    .heightIs(20);
    
    
    
    
    
    
    
    UIView *rightView = [[UIView alloc]init];
    rightView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [bottomView addSubview:rightView];
    
    rightView.sd_layout
    .rightSpaceToView(bottomView, 0)
    .topSpaceToView(bottomView, 0)
    .widthIs(SCREEN_WIDTH-150)
    .heightIs(kBottomBarHeight);
    
    
    UIButton *add = [[UIButton alloc]init];
    add.backgroundColor = COLOR_GREEN;
    [add setTitle:@"加入购物车" forState:UIControlStateNormal];
    [add setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightView addSubview:add];
    
    add.sd_layout
    .leftSpaceToView(rightView, 0)
    .topSpaceToView(rightView, 0)
    .widthIs((SCREEN_WIDTH-150)/2)
    .heightIs(kBottomBarHeight);
    
  
  
    UIButton *shop = [[UIButton alloc]init];
    shop.backgroundColor = COLOR_ORG;
    [shop setTitle:@"立即购买" forState:UIControlStateNormal];
    [shop addTarget:self action:@selector(shopAction:) forControlEvents:UIControlEventTouchUpInside];
    [shop setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightView addSubview:shop];
    
     shop.sd_layout
    .rightSpaceToView(rightView, 0)
    .topSpaceToView(rightView, 0)
    .widthRatioToView(add, 1)
    .heightIs(kBottomBarHeight);
    
    
    
}

#pragma mark - shop

- (void)shopAction:(UIButton *)btn {
    
    MsZzfqDetailSubmitOrderVC *vc = [[MsZzfqDetailSubmitOrderVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark - requestNetwork

/** 网络请求 详情 */
- (void)requestDetailInfo {
    
    MBProgressHUD *hud = [WHRNetwork showHudAtView:self.view message:@"正在加载..." isTextOnly:NO];
    ZX_WEAK_SELF
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"skuGoodsId"] = self.goodsId;
    params[@"skuId"] = self.specificationId;
    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_Store, URL_Store_selectGoodsInfo) success:^(id respose) {
        
        if ([respose[@"header"][@"statusCode"] integerValue] == 200) {
            
         
            weakSelf.detailModel1 = [AXHdzqMsZzfqDetailModel mj_objectWithKeyValues:respose[@"body"][@"goodsInfoVOs"]];
            /** 数组 */
            
            [self.tableView reloadData];
            
            
        } else if ([respose[@"header"][@"statusCode"] integerValue] != 200) {
            
            
        }
        [hud hideAnimated:YES];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        
    } failure:^(NSError *error) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
    
    
}






#pragma mark - tableviewDelegate & tableviewDatasource

//分区头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        
        
        ZX_WEAK_SELF
        self.detailsV = [[LZProductDetails alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
        self.detailsV.scrollOptBlock = ^(NSInteger index) {
            
            NSLog(@"第%d页",(int)index);
        };
        /** 点击视频播放 */
        weakSelf.detailsV.PlayVideoOptBlock = ^(BOOL isOK) {
            
            PlayerView *playerView = [[PlayerView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 300)];
            [self.detailsV addSubview:playerView];
            NSInteger indexBtn;
            NSInteger indexPlayerView;
            NSInteger indexViedo;
            for (UIView *view in self.detailsV.subviews) {
                if ([view isKindOfClass:[UIButton class]]) {
                    UIButton *button = (UIButton *)view;
                    if ([button.titleLabel.text isEqualToString:@"视频"]) {
                        indexBtn = [self.detailsV.subviews indexOfObject:view];
                    }
                    
                    if ([button.titleLabel.text isEqualToString:@"图片"]) {
                        indexViedo = [self.detailsV.subviews indexOfObject:view];
                    }
                }
                if ([view isKindOfClass:[PlayerView class]]) {
                    indexPlayerView = [self.detailsV.subviews indexOfObject:view];
                }
            }
            
            if (indexViedo < indexBtn) {
                [self.detailsV exchangeSubviewAtIndex:indexPlayerView withSubviewAtIndex:indexViedo];
            } else {
                [self.detailsV exchangeSubviewAtIndex:indexPlayerView withSubviewAtIndex:indexBtn];
            }

            
            
            
            //视频地址
            playerView.url = [NSURL URLWithString:@"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
            //播放
            [playerView playVideo];
            //返回按钮点击事件回调
            [playerView backButton:^(UIButton *button) {
                NSLog(@"返回按钮被点击");
            }];
            //播放完成回调
            [playerView endPlay:^{
                NSLog(@"播放完成");
            }];

         
            
            
            NSLog(@"点击播放");
        };
        
        weakSelf.detailsV.viedoPic = ^(NSInteger index) {
            if (index != 10) {
                
                for (UIView *view in self.detailsV.subviews) {

                    if ([view isKindOfClass:[PlayerView class]]) {
                        PlayerView *playerView = (PlayerView *)view;
                        [playerView pausePlay];
                        [view removeFromSuperview];
                    }
                }
                
            } else {
                
            }
        };
        [self.detailsV updateUIWithImageAndVideoArray:@[@"djasj",@"klasljdklja",@"dwedjj",@"223424"]];
        
        return self.detailsV;
    }
    
    return [UIView new];
    
  
    
    
}
/** 分区的高度 */

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return 300;
    }
    
    return 0;
    
}



- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0;
}



#pragma mark - tableviewDelegate & tableviewDatasource

/** cell 的选中事件 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 1) {
     
        WHRDiscountCouponVC *vc = [WHRDiscountCouponVC new];
        /** 传值 */
        vc.shopId = self.shopId;
        
        [self yc_bottomPresentController:vc presentedHeight:SCREEN_HEIGHT-200 completeHandle:^(BOOL presented) {
            if (presented) {
                NSLog(@"弹出了");
            }else{
                NSLog(@"消失了");
            }
        }];
        
    }
    
}



   

//分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

//分区高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    

    NSInteger section = indexPath.section;
    CGFloat height = 0.;
    
    if (section==0) {
        
        return [tableView cellHeightForIndexPath:indexPath model:self.modelsArray[indexPath.row] keyPath:@"model" cellClass:[WHRProductDetailsCell class] contentViewWidth:SCREEN_WIDTH];
        
    }else if(section==1){
        
        return 44;
        
    }else if(section==2){
        
        height = CGRectGetHeight(self.view.frame)-kBottomBarHeight-kTopBarHeight;
    }
    return height;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    //cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSInteger section  = indexPath.section;
    
    
   /** 商品详情 */
    if (section==0) {
        
        WHRProductDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:[WHRProductDetailsCell reuseID] forIndexPath:indexPath];
         cell.backgroundColor = [UIColor whiteColor];
         [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        self.detailModel1.endDatetime = self.fallTime;
         cell.model = self.detailModel1;
        /** 产品原价 */
        cell.productpriceLabel.text = [NSString stringWithFormat:@"活动价:%@",self.skillPrice];
        /** 倒计时 */
        return cell;
        
    }
    /** 领优惠券 */
    else if(section==1){
        
        WHRProductActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:[WHRProductActivityCell reuseID] forIndexPath:indexPath];
         cell.backgroundColor = [UIColor whiteColor];
         [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        return cell;
        
    }
    /** 评论、详情 */
    else if(section==2){
        
        NSArray *tabConfigArray = @[@{
                                        @"title":@"详情",
                                        @"view":@"WHRMSDetail",
                                        @"data":@"图文介绍的数据",
                                        @"position":@0
                                        },@{
                                        @"title":@"评论",
                                        @"view":@"WHRMSComment",
                                        @"data":@"商品详情的数据",
                                        @"position":@1
                                        }];
        
        YXTabView *tabView = [[YXTabView alloc] initWithTabConfigArray:tabConfigArray];
        tabView.msID = self.goodsId;
        [cell.contentView addSubview:tabView];
    }
    return cell;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    
    CGFloat tabOffsetY = [_tableView rectForSection:2].origin.y-kTopBarHeight;
    CGFloat offsetY = scrollView.contentOffset.y;
    _isTopIsCanNotMoveTabViewPre = _isTopIsCanNotMoveTabView;
    if (offsetY>=tabOffsetY) {
        scrollView.contentOffset = CGPointMake(0, tabOffsetY);
        _isTopIsCanNotMoveTabView = YES;
    }else{
        _isTopIsCanNotMoveTabView = NO;
    }
    if (_isTopIsCanNotMoveTabView != _isTopIsCanNotMoveTabViewPre) {
        if (!_isTopIsCanNotMoveTabViewPre && _isTopIsCanNotMoveTabView) {
            //NSLog(@"滑动到顶端");
            [[NSNotificationCenter defaultCenter] postNotificationName:kGoTopNotificationName object:nil userInfo:@{@"canScroll":@"1"}];
            _canScroll = NO;
        }
        if(_isTopIsCanNotMoveTabViewPre && !_isTopIsCanNotMoveTabView){
            //NSLog(@"离开顶端");
            if (!_canScroll) {
                scrollView.contentOffset = CGPointMake(0, tabOffsetY);
            }
        }
    }
}


#pragma mark - setter && getter

//- (NSMutableArray<AXHdzqMsZzfqDetailModel *> *)imagwViewArray {
//
//    if (!_imagwViewArray) {
//        _imagwViewArray = [NSMutableArray<AXHdzqMsZzfqDetailModel *> array];
//    }
//    return _imagwViewArray;
//
//}


@end
